package com.library.service.impl;

import com.library.Mapper.CategoryMapper;
import com.library.pojo.Category;
import com.library.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有分类
     * @return
     */
    @Override
    public List<Category> getCategory(String name) {
        List<Category> types = categoryMapper.getType(name);
        return types;
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */

    @Override
    public Category getById(Long id) {
        Category category = categoryMapper.getById(id);
        return category;
    }

    /**
     * 新增分类
     * @param category
     */
    @Override
    public void add(Category category) {
        category.setBookNumber(0);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    @Override
    public void delete(Long id) {
        Category category = categoryMapper.getById(id);
        Integer bookNumber = category.getBookNumber();
        if(!bookNumber.equals(0)){
            throw new RuntimeException("分类下有书籍，不能删除");
        }
        categoryMapper.delete(id);
    }
}
